<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 最多牌组数 -->
    <script>
      var maxGroupNumber = function (tiles) {
        let h = new Map()
        tiles.forEach(i => {
          if (h.get(i) == undefined) {
            h.set(i, 1)
          } else {
            h.set(i, h.get(i) + 1)
          }
        })
        let arr = [...h.entries()]
        arr.sort((a, b) => a[0] - b[0])
        if (arr.length < 3) {
          let res = 0
          arr.forEach(i => {
            res += Math.floor(i[1] / 3)
          })
          return res
        } else {
          let f = []
          for (let i = 0; i < 5; i++) {
            f[i] = []
          }
          f[0][0] = 0
          for (let i = 0; i < arr.length; i++) {
            let ff = []
            for (let j = 0; j < 5; j++) {
              ff[j] = []
            }
            for (let m = 0; m < 5; m++) {
              for (let n = 0; n < 5; n++) {
                for (let j = 0; j < 5; j++) {
                  if (arr[i][1] < j || f[m][n] == undefined) {
                    break
                  } else {
                    let min = 0
                    if (arr[i - 2] != undefined && arr[i][0] - arr[i - 2][0] == 2) {
                      min = Math.min(m, n, arr[i][1] - j)
                    }
                    for (let k = 0; k <= min; k++) {
                      ff[n - k][j] = Math.max(ff[n - k][j] || 0, k + f[m][n] + Math.floor((arr[i][1] - j - k) / 3))
                    }
                  }
                }
              }
            }
            f = ff
          }
          return f[0][0]
        }
      }
      console.log(maxGroupNumber([2, 2, 2, 3, 4]))
    </script>
  </body>
</html>
